document.addEventListener('DOMContentLoaded', function() {
    // 从URL获取文章ID
    const urlParams = new URLSearchParams(window.location.search);
    const mentalId = urlParams.get('mentalId');

    if (!mentalId) {
        alert('未找到文章ID');
        return;
    }

    // 从服务器获取文章数据
    fetch(`../php/home.php?mentalId=${mentalId}`)
        .then(response => response.json())
        .then(result => {
            if (result.success) {
                updateArticleContent(result.data);
            } else {
                alert(result.message || '获取文章失败');
            }
        })
        .catch(error => {
            console.error('获取文章数据出错:', error);
            alert('获取文章数据时发生错误');
        });

    // 更新页面内容
    function updateArticleContent(data) {
        document.title = data.title;
        document.querySelector('.article-title').textContent = data.title;
        document.getElementById('author').textContent = data.username;
        document.getElementById('publishTime').textContent = data.create_time;
        document.getElementById('articleContent').innerHTML = data.content;
        document.getElementById('articleImage').src = data.img;
    }
});
